﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace ElingOA6_DSJCJ.DAL
{
	/// <summary>
	/// 数据访问类:accManagerDir
	/// </summary>
	public partial class AccManagerDir
	{
		public AccManagerDir()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("FPreDirId", "t_accManagerDir"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int FPreDirId,int FYear,string FEntityNo,int FDirId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from t_accManagerDir");
			strSql.Append(" where FPreDirId=@FPreDirId and FYear=@FYear and FEntityNo=@FEntityNo and FDirId=@FDirId ");
			SqlParameter[] parameters = {
					new SqlParameter("@FPreDirId", SqlDbType.Int,4),
					new SqlParameter("@FYear", SqlDbType.Int,4),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FDirId", SqlDbType.Int,4)			};
			parameters[0].Value = FPreDirId;
			parameters[1].Value = FYear;
			parameters[2].Value = FEntityNo;
			parameters[3].Value = FDirId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(ElingOA6_DSJCJ.Model.AccManagerDir model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into t_accManagerDir(");
			strSql.Append("FPreDirId,FDirName,FDispSeq,FIsDisp,FYear,FEntityNo,FIsBase,FCreateEmplNo,FModifyEmplNo,FCreateDate,FModifyDate,FDirClsProp,FIsSecret,FLinkDirId)");
			strSql.Append(" values (");
			strSql.Append("@FPreDirId,@FDirName,@FDispSeq,@FIsDisp,@FYear,@FEntityNo,@FIsBase,@FCreateEmplNo,@FModifyEmplNo,@FCreateDate,@FModifyDate,@FDirClsProp,@FIsSecret,@FLinkDirId)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@FPreDirId", SqlDbType.Int,4),
					new SqlParameter("@FDirName", SqlDbType.NVarChar,200),
					new SqlParameter("@FDispSeq", SqlDbType.Int,4),
					new SqlParameter("@FIsDisp", SqlDbType.Int,4),
					new SqlParameter("@FYear", SqlDbType.Int,4),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FIsBase", SqlDbType.Int,4),
					new SqlParameter("@FCreateEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FModifyEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FCreateDate", SqlDbType.DateTime),
					new SqlParameter("@FModifyDate", SqlDbType.DateTime),
					new SqlParameter("@FDirClsProp", SqlDbType.NVarChar,2),
					new SqlParameter("@FIsSecret", SqlDbType.Char,1),
					new SqlParameter("@FLinkDirId", SqlDbType.Int,4)};
			parameters[0].Value = model.FPreDirId;
			parameters[1].Value = model.FDirName;
			parameters[2].Value = model.FDispSeq;
			parameters[3].Value = model.FIsDisp;
			parameters[4].Value = model.FYear;
			parameters[5].Value = model.FEntityNo;
			parameters[6].Value = model.FIsBase;
			parameters[7].Value = model.FCreateEmplNo;
			parameters[8].Value = model.FModifyEmplNo;
			parameters[9].Value = model.FCreateDate;
			parameters[10].Value = model.FModifyDate;
			parameters[11].Value = model.FDirClsProp;
			parameters[12].Value = model.FIsSecret;
			parameters[13].Value = model.FLinkDirId;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(ElingOA6_DSJCJ.Model.AccManagerDir model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update t_accManagerDir set ");
			strSql.Append("FDirName=@FDirName,");
			strSql.Append("FDispSeq=@FDispSeq,");
			strSql.Append("FIsDisp=@FIsDisp,");
			strSql.Append("FIsBase=@FIsBase,");
			strSql.Append("FCreateEmplNo=@FCreateEmplNo,");
			strSql.Append("FModifyEmplNo=@FModifyEmplNo,");
			strSql.Append("FCreateDate=@FCreateDate,");
			strSql.Append("FModifyDate=@FModifyDate,");
			strSql.Append("FDirClsProp=@FDirClsProp,");
			strSql.Append("FIsSecret=@FIsSecret,");
			strSql.Append("FLinkDirId=@FLinkDirId");
			strSql.Append(" where FDirId=@FDirId");
			SqlParameter[] parameters = {
					new SqlParameter("@FDirName", SqlDbType.NVarChar,200),
					new SqlParameter("@FDispSeq", SqlDbType.Int,4),
					new SqlParameter("@FIsDisp", SqlDbType.Int,4),
					new SqlParameter("@FIsBase", SqlDbType.Int,4),
					new SqlParameter("@FCreateEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FModifyEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FCreateDate", SqlDbType.DateTime),
					new SqlParameter("@FModifyDate", SqlDbType.DateTime),
					new SqlParameter("@FDirClsProp", SqlDbType.NVarChar,2),
					new SqlParameter("@FIsSecret", SqlDbType.Char,1),
					new SqlParameter("@FLinkDirId", SqlDbType.Int,4),
					new SqlParameter("@FDirId", SqlDbType.Int,4),
					new SqlParameter("@FPreDirId", SqlDbType.Int,4),
					new SqlParameter("@FYear", SqlDbType.Int,4),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
			parameters[0].Value = model.FDirName;
			parameters[1].Value = model.FDispSeq;
			parameters[2].Value = model.FIsDisp;
			parameters[3].Value = model.FIsBase;
			parameters[4].Value = model.FCreateEmplNo;
			parameters[5].Value = model.FModifyEmplNo;
			parameters[6].Value = model.FCreateDate;
			parameters[7].Value = model.FModifyDate;
			parameters[8].Value = model.FDirClsProp;
			parameters[9].Value = model.FIsSecret;
			parameters[10].Value = model.FLinkDirId;
			parameters[11].Value = model.FDirId;
			parameters[12].Value = model.FPreDirId;
			parameters[13].Value = model.FYear;
			parameters[14].Value = model.FEntityNo;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int FDirId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from t_accManagerDir ");
			strSql.Append(" where FDirId=@FDirId");
			SqlParameter[] parameters = {
					new SqlParameter("@FDirId", SqlDbType.Int,4)
			};
			parameters[0].Value = FDirId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int FPreDirId,int FYear,string FEntityNo,int FDirId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from t_accManagerDir ");
			strSql.Append(" where FPreDirId=@FPreDirId and FYear=@FYear and FEntityNo=@FEntityNo and FDirId=@FDirId ");
			SqlParameter[] parameters = {
					new SqlParameter("@FPreDirId", SqlDbType.Int,4),
					new SqlParameter("@FYear", SqlDbType.Int,4),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FDirId", SqlDbType.Int,4)			};
			parameters[0].Value = FPreDirId;
			parameters[1].Value = FYear;
			parameters[2].Value = FEntityNo;
			parameters[3].Value = FDirId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string FDirIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from t_accManagerDir ");
			strSql.Append(" where FDirId in ("+FDirIdlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select FDirId,FPreDirId,FDirName,FDispSeq,FIsDisp,FYear,FEntityNo,FIsBase,FCreateEmplNo,FModifyEmplNo,FCreateDate,FModifyDate,FDirClsProp,FIsSecret,FLinkDirId ");
			strSql.Append(" FROM t_accManagerDir ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" FDirId,FPreDirId,FDirName,FDispSeq,FIsDisp,FYear,FEntityNo,FIsBase,FCreateEmplNo,FModifyEmplNo,FCreateDate,FModifyDate,FDirClsProp,FIsSecret,FLinkDirId ");
			strSql.Append(" FROM t_accManagerDir ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM t_accManagerDir ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.FDirId desc");
			}
			strSql.Append(")AS Row, T.*  from t_accManagerDir T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "t_accManagerDir";
			parameters[1].Value = "FDirId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  已经使用的

        /// <summary>
        /// 获取系统配置的信息类别
        /// <creator>sunlj</creator>
        /// <date>2013-09-07</date>
        /// </summary>
        /// <param name="entityNo">公司编号</param>
        /// <returns></returns>
        public DataSet GetACCDir(string entityNo,int dirId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT DISTINCT FDirId ,FPreDirId ,FDirName ,DirLevel , FDispSeq FROM (");
            strSql.Append("SELECT DISTINCT FDirId ,FPreDirId ,FDirName ,DirLevel , FDispSeq FROM dbo.fn_GetAccDir(@FEntityNo,@dirId) ");
            strSql.Append(" UNION ALL ");
            strSql.Append("SELECT DISTINCT FDirId ,FPreDirId ,FDirName ,DirLevel , FDispSeq FROM dbo.fn_GetAccDirPre(@FEntityNo,@dirId) ");

            strSql.Append(") a ");
            strSql.Append(" ORDER BY DirLevel,FDispSeq");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@dirId",SqlDbType.Int)		};
            parameters[0].Value = entityNo;
            parameters[1].Value = dirId;
            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_DSJCJ.Model.AccManagerDir GetModel(int FDirId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FDirId,FPreDirId,FDirName,FDispSeq,FIsDisp,FYear,FEntityNo,FIsBase,FCreateEmplNo,FModifyEmplNo,FCreateDate,FModifyDate,FDirClsProp,FIsSecret,FLinkDirId from t_accManagerDir ");
            strSql.Append(" where FDirId=@FDirId");
            SqlParameter[] parameters = {
					new SqlParameter("@FDirId", SqlDbType.Int,4)
			};
            parameters[0].Value = FDirId;

            ElingOA6_DSJCJ.Model.AccManagerDir model = new ElingOA6_DSJCJ.Model.AccManagerDir();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_DSJCJ.Model.AccManagerDir DataRowToModel(DataRow row)
        {
            ElingOA6_DSJCJ.Model.AccManagerDir model = new ElingOA6_DSJCJ.Model.AccManagerDir();
            if (row != null)
            {
                if (row["FDirId"] != null && row["FDirId"].ToString() != "")
                {
                    model.FDirId = int.Parse(row["FDirId"].ToString());
                }
                if (row["FPreDirId"] != null && row["FPreDirId"].ToString() != "")
                {
                    model.FPreDirId = int.Parse(row["FPreDirId"].ToString());
                }
                if (row["FDirName"] != null)
                {
                    model.FDirName = row["FDirName"].ToString();
                }
                if (row["FDispSeq"] != null && row["FDispSeq"].ToString() != "")
                {
                    model.FDispSeq = int.Parse(row["FDispSeq"].ToString());
                }
                if (row["FIsDisp"] != null && row["FIsDisp"].ToString() != "")
                {
                    model.FIsDisp = int.Parse(row["FIsDisp"].ToString());
                }
                if (row["FYear"] != null && row["FYear"].ToString() != "")
                {
                    model.FYear = int.Parse(row["FYear"].ToString());
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FIsBase"] != null && row["FIsBase"].ToString() != "")
                {
                    model.FIsBase = int.Parse(row["FIsBase"].ToString());
                }
                if (row["FCreateEmplNo"] != null)
                {
                    model.FCreateEmplNo = row["FCreateEmplNo"].ToString();
                }
                if (row["FModifyEmplNo"] != null)
                {
                    model.FModifyEmplNo = row["FModifyEmplNo"].ToString();
                }
                if (row["FCreateDate"] != null && row["FCreateDate"].ToString() != "")
                {
                    model.FCreateDate = DateTime.Parse(row["FCreateDate"].ToString());
                }
                if (row["FModifyDate"] != null && row["FModifyDate"].ToString() != "")
                {
                    model.FModifyDate = DateTime.Parse(row["FModifyDate"].ToString());
                }
                if (row["FDirClsProp"] != null)
                {
                    model.FDirClsProp = row["FDirClsProp"].ToString();
                }
                if (row["FIsSecret"] != null)
                {
                    model.FIsSecret = row["FIsSecret"].ToString();
                }
                if (row["FLinkDirId"] != null && row["FLinkDirId"].ToString() != "")
                {
                    model.FLinkDirId = int.Parse(row["FLinkDirId"].ToString());
                }
            }
            return model;
        }


        /// <summary>
        /// 获取系统配置的信息类别
        /// <creator>sunlj</creator>
        /// <date>2013-09-07</date>
        /// </summary>
        /// <param name="entityNo">公司编号</param>
        /// <returns></returns>
        public DataSet GetACCDirALL(string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT FYear,FDirId,FDirName,FDispSeq FROM dbo.t_accManagerDir WHERE FEntityNo='06' AND FPreDirId IS NULL ");
            strSql.Append(" ORDER BY FYear DESC,FDispSeq ");
            SqlParameter[] parameters = {
					new SqlParameter("@EntityNo", SqlDbType.NVarChar,12)	};
            parameters[0].Value = entityNo;
            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }

        #endregion  已经使用的
    }
}

